![](data:image/svg+xml;utf8,svg%20xmlns='http://www.w3.org/2000/svg'%20width='2083'%20height='1053'/svg) 在这篇文章中呢,我将为大家分享Kotlin与Java上的主要异同,希望能够帮助到各位小伙伴利用现有的Java知识快速上手Kotlin开发。 本文出自教程《移动端架构师成长体系课》——带你完成从普通工程师到架构师的蜕变。打印日志JavaSystem.out.print("hello world");
System.out.println("hello world");Kotlinprint("hello world")
println("hello world")定义变量与常量常JavaString name = "hello world";
final String name = "hello world";Kotlinvar name = "hello world"
val name = "hello world"null声明JavaString otherName;
otherName = null;Kotlinvar otherName : String?
otherName = null空判断Javaif (text != null) {
int length = text.length();
}Kotlintext?.let {
val length = text.length
}
// or simply
val length = text?.length字符串拼接JavaString firstName = "Android";
String lastName = "Architect";
String message = "My name is: " + firstName + " " + lastName;Kotlinval firstName = "Android"
val lastName = "Architect"
val message = "My name is: $firstName $lastName"换行JavaString text = "First Line\n" +
"Second Line\n" +
"Third Line";Kotlinval text = """
|First Line
|Second Line
|Third Line
""".trimMargin()三元表达式JavaString text = x > 5 ? "x > 5" : "x 5"
else "x |